/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package storymotion.user;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import storymotion.dao.DAO;

/**
 *
 * @author Leonardo Oliveira Moreira
 */
public class UserDAO extends DAO {

    public UserDAO() throws SQLException {
    }

    public void insert(User user) throws SQLException {
        if (user == null || user.getName() == null || user.getLogin() == null || user.getPassword() == null || user.getName().trim().length() == 0 || user.getLogin().trim().length() == 0 || user.getPassword().trim().length() == 0) {
            throw new SQLException("Could not perform this action");
        }
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO \"user\" (name, login, password) VALUES (?, ?, ?)");
            preparedStatement.setString(1, user.getName());
            preparedStatement.setString(2, user.getLogin());
            preparedStatement.setString(3, user.getPassword());
            int result = preparedStatement.executeUpdate();
            if (result != 1) {
                throw new SQLException("Could not perform this action");
            }
            preparedStatement.close();
        } catch (SQLException ex) {
            throw ex;
        }
    }

    public void update(User user) throws SQLException {
        if (user == null || user.getId() == null || user.getName() == null || user.getLogin() == null || user.getPassword() == null || user.getName().trim().length() == 0 || user.getLogin().trim().length() == 0 || user.getPassword().trim().length() == 0) {
            throw new SQLException("Could not perform this action");
        }
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("UPDATE \"user\" SET name = ?, login = ?, password = ? WHERE id = ?");
            preparedStatement.setString(1, user.getName());
            preparedStatement.setString(2, user.getLogin());
            preparedStatement.setString(3, user.getPassword());
            preparedStatement.setLong(4, user.getId());            
            int result = preparedStatement.executeUpdate();
            if (result != 1) {
                throw new SQLException("Could not perform this action");
            }
            preparedStatement.close();
        } catch (SQLException ex) {
            throw ex;
        }
    }

    public void delete(long id) throws SQLException {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM \"user\" WHERE id = ?");
            preparedStatement.setLong(1, id);
            int result = preparedStatement.executeUpdate();
            if (result != 1) {
                throw new SQLException("Could not perform this action");
            }
            preparedStatement.close();
        } catch (SQLException ex) {
            throw ex;
        }
    }

    public List<User> get(User user) {
        List<User> result = new ArrayList<User>();

        return result;
    }

    public User getById(long id) throws SQLException {
        User user = null;
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT id, name, login, password FROM \"user\" WHERE id = ?");
            preparedStatement.setLong(1, id);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                user = new User();
                user.setId(resultSet.getLong("id"));
                user.setName(resultSet.getString("name"));
                user.setLogin(resultSet.getString("login"));
                user.setPassword(resultSet.getString("password"));
            }
            resultSet.close();
            preparedStatement.close();
        } catch (SQLException ex) {
            throw ex;
        }
        return user;
    }
    
    public User getByLogin(String login) throws SQLException {
        if (login == null || login.trim().length() == 0) {
            throw new SQLException("Could not perform this action");
        }
        User user = null;
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT id, name, login, password FROM \"user\" WHERE login = ?");
            preparedStatement.setString(1, login);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                user = new User();
                user.setId(resultSet.getLong("id"));
                user.setName(resultSet.getString("name"));
                user.setLogin(resultSet.getString("login"));
                user.setPassword(resultSet.getString("password"));
            }
            resultSet.close();
            preparedStatement.close();
        } catch (SQLException ex) {
            throw ex;
        }
        return user;
    }
}
